<?php
// This example script demonstrates Imps most important basic features



// Before running, check the paths in the demo_initialization file
include('demo_initialization.php');



$imp = new Imp($smarty);



// First, we define some invalidators.  If these rules fire, the form will go back to the user for checking
// The format for declaring invalidators is:
// invalidator($requirement_name, $invalidating_function_name, $invalidating_function_arguments, $result_matching_directive, $corroborative_result);
// where
//   string $requirement_name                is a handle that links many invalidators to a single form constraint group.  many invalidators can invalidate a single requirement.
//   string $invalidating_function_name      is the name of the function to call during the validation process.  the global namespace will be searched first, followed by the Imp namespace.  function handles of the form array($class_name, $method_name) are also accepted
//   array  $invalidating_function_arguments is the set of arguments to be passed to the invalidation function.  if omitted, the $invalidating_function_name is assumed to be a pre-calculated result, and is used for comparison instead of running any invalidation at all.
//   mixed  $result_matching_directive       (default: IMP_MATCH_BOOLEAN) is a parameter that indicates the type of comparison that will be done between the invalidating function's result and the corroborative_result
//     options are
//       IMP_MATCH_BOOLEAN (default): casts the result as a bool before comparing it to the corroborative_result
//       IMP_MATCH_INEXACT          : compares the result to the corroborative_result using the == operator
//       IMP_MATCH_EXACT            : compares the result to the corroborative_result using the === operator
//   mixed  $corroborative_result            (default: true) is the result that gets compared to the invalidating function's result.  if the results match (as defined by the $result_matching_directive parameter), then the associated requirement is cast as invalid
// NOTE The 'imp_field' object is a proxy that gets replaced with the submission variable specified.  This allows for suspended and conditional processing of the invalidator functions
$imp->invalidator( 'name_exists'      , 'empty'            , array(imp_field('name'))                      ); // (NOTE: since 'empty' is a language construct instead of a function, this invalidator will be caught by Imp before atempting to proces, and the expected behavior will be simulated)
$imp->invalidator( 'valid_birthdate'  , 'invalid_date'     , array(imp_field('birthdate'))                 ); // Since this validation function name is not in the global namespace, Imp it automatically searches the library of functions in the imp namespace
$imp->invalidator( 'password_exists'  , 'empty'            , array(imp_field('pass1'))                     );
$imp->invalidator( 'password_mismatch', 'password_mismatch', array(imp_field('pass1'), imp_field('pass2')) ); // This custom function (defined below) is available in the global namespace - any standard PHP functions may be used here as well
function password_mismatch( $first, $second ){ return (strcmp($first, $second)!==0); } // This custom function, mentioned in the line above, must be defined at some point before the invalidators get run (during $imp->validate())



$smarty->assign('occupation_list', array('job_1' => 'Plumber', 'job_2'=>'Electrician', 'job_3'=>'Other')); // This list will be used to automatically generate a dropdown list.



$imp->preset('occupation', 'job_2'); // Once the occupation_list array is rendered into a dropdown box called 'occupation', This will make Electrician the default value until changed by the user



// With the setup done, we can create a very simple form flow logic of our own
if( $imp->submitted() ){ // check if the form was submitted.
  echo'YOU HAVE SUBMITTED THIS FORM...<br/>';
  if( $imp->validate() ){ // check to see if it was valid
    echo 'AND IT WAS VALID!<br/>';
    echo 'Your birthday is '.$imp->submission('birthdate'); // note that this value went into and out of the form without needing to be processed at all.  Retrieving by field name on specially generated fields (called cloaks) produces a compiled result.
    exit();
  }
  ECHO 'BUT THERE WERE ERRORS:<BR/>';
}



$smarty->display('simple_form.tpl');

?>
